/**
 * 
 */
package org.ycsoft.metroid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ycsoft.MetroidReader.LineRecord;
import org.ycsoft.MetroidReader.MetroidMetadata;
import org.ycsoft.MetroidReader.MetroidReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * @author Lkster
 *
 */
public class LineDetails extends Activity {
	
	

	public TextView title,city, card,freq;  //Card as in cardinality
	public ListView stopsList;
	public ImageView meanIcon;
	public int nbStops,lineId;
	//public boolean errorInd;
	public List<String> lineStops;
	public String cityName;
	public String mapFile,lineName;
	private ProgressDialog dlgProgress;
	public Handler handler;
	public MetroidReader reader;
	
	
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		//errorInd=false;
		setContentView(R.layout.line);
		title=(TextView)findViewById(R.id.lineTitle);
		stopsList=(ListView)findViewById(R.id.allStops);
		meanIcon=(ImageView)findViewById(R.id.meanPic);
		city=(TextView)findViewById(R.id.lineCity);
		card=(TextView)findViewById(R.id.lineCard);
		Bundle extras = getIntent().getExtras();
		if (extras != null) 
		{
			mapFile=extras.getString(Commons.KEY_MAP);
			lineName=extras.getString(Commons.KEY_LINE_NAME);
			lineId=Integer.parseInt(extras.getString(Commons.KEY_LINE_ID));
			cityName=extras.getString(Commons.KEY_CITY);
			title.append(lineName);
			city.append(" "+cityName);
			Log.e(Commons.TAG,lineName);
			handler = new Handler();
			Thread lineLoader=new Thread(new Runnable()
			{
				public void run() 
				{
					// load map data
					try {
						loadLine();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			});
			lineLoader.start();	
		}
		else
		{
			//Default load nothing
			//errorInd=true;
			finish();
		}
		
	}
	
	
	public void loadLine() throws IOException
	{
		
			synchronized (handler) 
			{
				handler.post(new Runnable() 
				{
					public void run() 
					{
						dlgProgress = ProgressDialog.show(LineDetails.this,
								Commons.TAG, getResources().getString
								(R.string.load_line), true, false);
					}
						
				});
				
			}
			reader=new MetroidReader(mapFile);
			reader.loadIndexes(true);
			final MetroidMetadata meta=reader.getMetadata();
			final LineRecord curLine=reader.getLineFromId(2);
			List<Integer> stopsIds=curLine.getStopsList();
			lineStops=new ArrayList<String>();
			for(int item:stopsIds)
				lineStops.add(reader.getStopFromId(item).getStopName());
			nbStops=stopsIds.size();
			final ArrayAdapter<String> items = new ArrayAdapter<String>(this,
					R.layout.listbold, lineStops);
			reader.closeStream();
			synchronized (handler) 
			{
				handler.post(new Runnable() 
				{
					public void run() 
					{
						/*if(meta.getMean().equalsIgnoreCase("metro"))
							meanIcon.setImageResource(R.drawable.ln);
						else if(meta.getMean().equalsIgnoreCase("metro"))
							meanIcon.setImageResource(R.drawable.bus);
						else
							meanIcon.setImageResource(R.drawable.icon);*/
						card.append(String.valueOf(nbStops)+Commons.STP);
						freq=(TextView)findViewById(R.id.linefreq);
						freq.append(String.valueOf(curLine.getFrequency()/60)
								+"mn");
						stopsList.setAdapter(items);
						stopsList.setOnItemClickListener(goToStopDetails);
					}
				});
			}
			dlgProgress.dismiss();

	}
		

	private OnItemClickListener goToStopDetails=new OnItemClickListener()
	{
		public void onItemClick(AdapterView arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			showStop(position);
		}
		
	};
	
	public void showStop(int pos)
	{
		Intent i=new Intent(this,StopDetails.class);
		i.putExtra(Commons.KEY_STOP, lineStops.get(pos));
		i.putExtra(Commons.KEY_CITY, cityName);
		i.putExtra(Commons.KEY_MAP, mapFile);
		startActivity(i);
		//finish();
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		try{
			if(reader != null && !reader.isStreamClosed())
			{
				reader.closeStream();
			}
		}catch(Exception e){
			Log.e(Commons.TAG,e.getMessage());
		}
	}
	
	public void onpause()
	{
		super.onPause();
		try{
			if(reader != null && !reader.isStreamClosed())
			{
				reader.closeStream();
			}
		}catch(Exception e){
			Log.e(Commons.TAG,e.getMessage());
		}
	}
	
}
